package com.jic.market.controller.rest;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.market.api.rest.LotteryRestApi;
import com.jic.market.common.util.BeanUtil;
import com.jic.market.entity.LotteryDrawRecord;
import com.jic.market.enums.LotteryDrawResultCode;
import com.jic.market.exception.LotteryDrawException;
import com.jic.market.request.rest.lottery.LotteryActivityInfoRestRequest;
import com.jic.market.request.rest.lottery.LotteryReceiverRequest;
import com.jic.market.request.rest.lottery.WinningRecordsRestRequest;
import com.jic.market.response.rest.lottery.LotteryActivityInfoRestResponse;
import com.jic.market.response.rest.lottery.LotteryDrawRestResponse;
import com.jic.market.response.rest.lottery.LotteryWinningRecordsResponse;
import com.jic.market.service.rest.lottery.LotteryActivityInfoRestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: MJ
 * @date: 2020-04-07
 * @remark:
 */
@RestController
@RequestMapping("/rest/lottery")
@Api(tags = {"抽奖H5接口"})
@Slf4j
public class LotteryRestController implements LotteryRestApi {

    @Resource
    private LotteryActivityInfoRestService lotteryActivityInfoRestService;

    //是否返回模板信息配置
    public static final int PAGE_TEMP_1 = 1;

    public static final int PAGE_TEMP_2 = 2;


    @Override
    @PostMapping("/lotteryInfo")
    @ApiOperation(value = "抽奖活动信息")
    public RestResult<LotteryActivityInfoRestResponse> getLotteryAwardsList(@RequestBody LotteryActivityInfoRestRequest request) {
        try {
            LotteryActivityInfoRestResponse restResponse = lotteryActivityInfoRestService.queryAwardsListByThemeCode(request,PAGE_TEMP_1);
            return RestResult.success(restResponse);
        }catch (LotteryDrawException e){
            log.info(e.getMessage());
            return RestResult.error(e.getRespCode(),e.getLocalizedMessage());
        }
    }


    @Override
    @PostMapping("/drawLottery")
    @ApiOperation(value = "点击抽奖")
    public RestResult<LotteryDrawRestResponse> drawLottery(@RequestBody LotteryActivityInfoRestRequest request) {
        LotteryDrawRestResponse lotteryDrawRestResponse = new LotteryDrawRestResponse();
        try {
            lotteryDrawRestResponse = lotteryActivityInfoRestService.initiateLottery(request);
            return RestResult.success(lotteryDrawRestResponse);
        }catch (LotteryDrawException e){
            log.info(e.getMessage());
            lotteryDrawRestResponse.setDrawResultCode(LotteryDrawResultCode.LOTTERY_DRAW_RESULT_NOT_WINNING.getCode());
            lotteryDrawRestResponse.setDrawResultDesc(LotteryDrawResultCode.LOTTERY_DRAW_RESULT_NOT_WINNING.getMsg());
            return RestResult.success(lotteryDrawRestResponse);
        }
    }


    @Override
    @PostMapping("/winningRecord")
    @ApiOperation(value = "我的中奖记录")
    public RestResult<PageResult<LotteryWinningRecordsResponse>> winningRecord(@RequestBody WinningRecordsRestRequest request) {
        try {
            log.debug("queryLotteryThemeList " + request);
            Page page = new Page();
            page.setLimit(request.getLimit());
            page.setStart(request.getStart());
            PageResult<LotteryWinningRecordsResponse> result = lotteryActivityInfoRestService.getUserDrawRecordList(request, page);
            return RestResult.success(result);
        }catch (Exception e){
            log.info(e.getMessage());
            return RestResult.error("9999","获取抽奖信息失败");
        }
    }

    @Override
    @PostMapping("/saveReceiverAddress")
    @ApiOperation(value = "保存中奖收货地址")
    public RestResult<String> saveReceiverAddress(@RequestBody LotteryReceiverRequest request) {
        try {
            log.debug("queryLotteryThemeList " + request);
            RestResult restResult = lotteryActivityInfoRestService.saveReceiverAddress(request);
            return restResult;
        }catch (LotteryDrawException e){
            log.info(e.getMessage());
            return RestResult.error(e.getRespCode(),e.getLocalizedMessage());
        }
    }

}
